﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
//using System.Math;

namespace reseaux_neurones
{
    class Neurone
    {
     
        

        /* input  hidden neuronal */
        private double[] valueInputHidden = new double[4];   //ordre : age:annee:ganglion:seuil
        private double[] weigthInputHidden = new double[4];  

        /* input final neuronal */
        private Neurone[] inputHiddenNeuronal;


        /* Hidden */
        private double hiddenPotential;
        private double hiddenOutput;
        


        /* Final */
        private double[] weigthInputFinal;   
        private double finalPotential =0;
        private double finalOutput = 0;
        private double finalError = 0;

 



        /* Création Neurone Cachés */
        public Neurone()
        {
            weigthInputFinal = new double[Run.NombreNeurones];
            //random weigth
             Random random = new Random();

            //hidden
             for (int i=0;i<4;i++)
             {
                 Thread.Sleep(20);
                 weigthInputHidden[i] = random.NextDouble();
             }

            //final
             for (int n = 0; n < Run.NombreNeurones;n++ )
             {
                 Thread.Sleep(20);

                 weigthInputFinal[n] = random.NextDouble();
             }
                 
        }
    
        /* Création du neurone avec des poids directement */
        public Neurone(double p_age, double p_annee, double p_ganglion, double p_seuil)
        {
            weigthInputHidden[0] = p_age;
            weigthInputHidden[1] = p_annee;
            weigthInputHidden[2] = p_ganglion;
            weigthInputHidden[3] = p_seuil;

        }


/* Hidden */

        /* Input  */
        public void setInputHidden(int in_age,int in_anne,int in_gang)
        {
            valueInputHidden[0] = in_age;
            valueInputHidden[1] = in_anne;
            valueInputHidden[2] = in_gang;
        }

        /*  potential */
        public void findPotentialHidden()
        {
            this.hiddenPotential = valueInputHidden[0] * weigthInputHidden[0] + valueInputHidden[1] * weigthInputHidden[1] + valueInputHidden[2] * weigthInputHidden[2] - weigthInputHidden[3];
        }
         
        /* Output  */
        public void findHiddenOutput()
        {
            this.hiddenOutput = 1 / (1 + Math.Exp(-this.hiddenPotential));         
        }


/* Final */

        /* input */
        public void setInputFinal(Neurone[] _inputNeuronal)
        {
            this.inputHiddenNeuronal = _inputNeuronal;
        }     

        /* potential */
        public void findPotentialFinal()
        {
            for (int i = 0; i < this.inputHiddenNeuronal.Length; i++)
            {
                this.finalPotential += inputHiddenNeuronal[i].hiddenOutput * this.weigthInputFinal[i];
            }                 
        }
        
        /* output */
        public void findFinalOutput()
        {
            this.finalOutput = 1 / (1 + Math.Exp(-this.finalPotential));           
        }

        /* error */
        public void findError(int _sortieAttendu)
        {
            this.finalError = Math.Abs((_sortieAttendu-1) - this.finalOutput);          
        }

/* Get */
        public double getFinalError()
        {

            return this.finalError;
        }

        public double getSortieCache()
        {
            return this.hiddenOutput;
        }

        public double getSortieFinal()
        {
            return this.finalOutput;
        }

     
    }
}
